package com.ruoyi.product.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.product.domain.TbProdAtta;
import com.ruoyi.product.domain.TbProdInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.product.domain.TbProd;
import com.ruoyi.product.service.ITbProdService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品列表Controller
 *
 * @author ruoyi
 * @date 2025-08-05
 */
@RestController
@RequestMapping("/prod")
@Slf4j
public class TbProdController extends BaseController {
    @Autowired
    private ITbProdService tbProdService;

    /**
     * 查询商品列表列表
     */
    @RequiresPermissions("product:prod:list")
    @GetMapping("/list")
    public TableDataInfo list(TbProd tbProd) {
        startPage();
        List<TbProd> list = tbProdService.selectTbProdList(tbProd);
        return getDataTable(list);
    }

    /**
     * 导出商品列表列表
     */
    @RequiresPermissions("product:prod:export")
    @Log(title = "商品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbProd tbProd) {
        List<TbProd> list = tbProdService.selectTbProdList(tbProd);
        ExcelUtil<TbProd> util = new ExcelUtil<TbProd>(TbProd.class);
        util.exportExcel(response, list, "商品列表数据");
    }

    /**
     * 获取商品列表详细信息
     */
    @RequiresPermissions("product:prod:query")
    @GetMapping(value = "/{prodId}")
    public AjaxResult getInfo(@PathVariable("prodId") Integer prodId) {
        return success(tbProdService.selectTbProdByProdId(prodId));
    }

    /**
     * 新增商品列表
     */
    @RequiresPermissions("product:prod:add")
    @Log(title = "商品列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbProd tbProd) {
        return toAjax(tbProdService.insertTbProd(tbProd));
    }

    /**
     * 修改商品列表
     */
    @RequiresPermissions("product:prod:edit")
    @Log(title = "商品列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbProd tbProd) {
        return toAjax(tbProdService.updateTbProd(tbProd));
    }

    /**
     * 删除商品列表
     */
    @RequiresPermissions("product:prod:remove")
    @Log(title = "商品列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{prodIds}")
    public AjaxResult remove(@PathVariable Integer[] prodIds) {
        return toAjax(tbProdService.deleteTbProdByProdIds(prodIds));
    }


    /**
     * @author: 鑫仔
     * @description: 添加商品附件
     * @params: []
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     * @date: 2025/8/6 19:33
     */
    @PostMapping("addProdAtta")
    public AjaxResult addProdAtta(
            @RequestBody TbProdAtta prodAtta
    ) {
        return tbProdService.addProdAtta(prodAtta);
    }


    /**
     * @author: 鑫仔
     * @description: 商品SKU
     * @params: [com.ruoyi.product.domain.TbProdInfo]
     * @return: com.ruoyi.common.core.web.page.TableDataInfo
     * @date: 2025/8/7 10:41
     */
    @PostMapping("getProdInfo")
    public TableDataInfo getProdInfo(
            @RequestBody TbProdInfo prodInfo
    ) {

        PageHelper.startPage(prodInfo.getPageNum(), prodInfo.getPageSize());
        List<TbProdInfo> list = tbProdService.getProdInfo(prodInfo);
        return getDataTable(list);
    }

    /**
     * @author: 鑫仔
     * @description: 添加商品SKU
     * @params: []
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     * @date: 2025/8/7 19:25
     */
    @PostMapping("addProdInfo")
    public AjaxResult addProdInfo(
            @RequestBody TbProdInfo prodInfo
    ) {
        return tbProdService.addProdInfo(prodInfo);
    }

    /**
     * @author: 鑫仔
     * @description: 创建索引
     * @params: []
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     * @date: 2025/8/7 20:08
     */
    @PostMapping("createIndexEs")
    public AjaxResult createIndexEs() {
        return tbProdService.createIndexEs();
    }


    /**
     * @author: 鑫仔
     * @description: 数据同步到Es
     * @params: []
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     * @date: 2025/8/7 20:46
     */
    @PostMapping("syncProdIndex")
    public AjaxResult syncProdIndex() {
        return tbProdService.syncProdIndex();
    }


    /**
     * @author: 鑫仔
     * @description: 从Es中查询商品数据
     * @params: []
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     * @date: 2025/8/7 20:51
     */
    @RequiresPermissions("product:prod:list")
    @GetMapping("/getProdListByEs")
    public TableDataInfo getProdListByEs(
            HttpServletRequest request,
            TbProd tbProd
    ) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");

        TableDataInfo data = tbProdService.getProdListByEs(tbProd, Integer.valueOf(pageNum), Integer.valueOf(pageSize));
        return data;
    }

}
